package com.pfsbuilder.Dialogs;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.pfsbuilder.CommonLib;
import com.pfsbuilder.PExceptionDialog;
import com.pfsbuilder.algorithm.PFSReadImageAlgorithm;

public class ReadDialog extends JDialog implements Runnable {
	JPanel mainPanel = new JPanel();
	JPanel jpanel2 = new JPanel();
	JScrollPane jscrollPane1 = new JScrollPane();
	JTable jtable1 = new JTable();
	JProgressBar progressBar1 = new JProgressBar();
	JPanel controlPanel = new JPanel();
	JButton pauseButton = new JButton();
	BorderLayout borderLayout1 = new BorderLayout();
	BorderLayout borderLayout2 = new BorderLayout();
	ReadDialog_TableModel readDialogTableModel = new ReadDialog_TableModel();
	Table_CellRenderer tableCellRenderer = new Table_CellRenderer();
	JCheckBox showErrorOnlyCheckBox = new JCheckBox();
	JButton closeButton = new JButton();
	ReadDialog_TableCellRenderer readDialogTableCellRenderer = new ReadDialog_TableCellRenderer();
	PFSReadImageAlgorithm pfsReadImageAlgorithm = new PFSReadImageAlgorithm();
	JLabel infoLabel = new JLabel();

	public ReadDialog(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		try {
			jbInit();
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	private void jbInit() throws Exception {
		jpanel2.setLayout(borderLayout1);
		mainPanel.setLayout(borderLayout2);
		pauseButton.setText("Pause");
		pauseButton.addActionListener(new PReadDialog_pPauseButton_actionAdapter(this));
		jtable1.setModel(readDialogTableModel);
		showErrorOnlyCheckBox.setText("Show error only");
		showErrorOnlyCheckBox.addActionListener(new PReadDialog_pShowErrorOnlyCheckBox_actionAdapter(this));
		closeButton.setText("Close");
		closeButton.addActionListener(new PReadDialog_pCloseButton_actionAdapter(this));
		controlPanel.add(infoLabel);
		controlPanel.add(showErrorOnlyCheckBox);
		controlPanel.add(pauseButton);
		controlPanel.add(closeButton);
		jscrollPane1.getViewport().add(jtable1);
		jpanel2.add(jscrollPane1, java.awt.BorderLayout.CENTER);
		jpanel2.add(progressBar1, java.awt.BorderLayout.SOUTH);
		mainPanel.add(controlPanel, java.awt.BorderLayout.SOUTH);
		mainPanel.add(jpanel2, java.awt.BorderLayout.CENTER);
		this.getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
		jtable1.setDefaultRenderer(String.class, tableCellRenderer);
		jtable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		jtable1.getColumnModel().getColumn(0).setPreferredWidth(500);
		jtable1.getColumnModel().getColumn(1).setPreferredWidth(100);
		progressBar1.setVisible(false);

		jtable1.getColumnModel().getColumn(0).setCellRenderer(readDialogTableCellRenderer);
		jtable1.getColumnModel().getColumn(1).setCellRenderer(readDialogTableCellRenderer);

		setSize(640, 550);
		CommonLib.centerDialog(this);
		new Thread(this).start();
	}

	public void run() {
		try {
			if (com.pfsbuilder.Global.GUI) {
				pfsReadImageAlgorithm.readPFS(this, this.jtable1, this.readDialogTableModel, this.jscrollPane1, this.progressBar1, this.pauseButton, this.infoLabel);
			} else {
				pfsReadImageAlgorithm.readPFS();
			}
			closeButton.setText("Finish");
		} catch (Exception ex) {
			ex.printStackTrace();
			if (com.pfsbuilder.Global.GUI) {
				new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
			}
		}
	}

	// public void run() {
	// try {
	// readPFS();
	// pCloseButton.setText("Finished");
	// pPauseButton.setVisible(false);
	// } catch (Exception ex) {
	// ex.printStackTrace();
	// if (PFSBuilder.Globals.GUI) {
	// new PExceptionDialog(ex,
	// PFSBuilder.Globals.sessionNumber).setVisible(true);
	// }
	// }
	// }

	public void pPauseButton_actionPerformed(ActionEvent e) {
		if (pauseButton.getText().equals("Pause")) {
			pauseButton.setText("Resume");
		} else {
			pauseButton.setText("Pause");
		}
	}

	public void pShowErrorOnlyCheckBox_actionPerformed(ActionEvent e) {
		this.readDialogTableModel.setShowErrorOnly(showErrorOnlyCheckBox.isSelected());
	}

	public void pCloseButton_actionPerformed(ActionEvent e) {
		closeButton.setEnabled(false);
		pfsReadImageAlgorithm.shouldStop = true;
		while (pauseButton.isVisible() && !pauseButton.getText().equals("Resume")) {
			try {
				Thread.currentThread().sleep(1000);
			} catch (Exception ex) {
				ex.printStackTrace();
				if (com.pfsbuilder.Global.GUI) {
					new PExceptionDialog(ex, com.pfsbuilder.Global.sessionNumber).setVisible(true);
				}
			}
		}
		this.setVisible(false);
	}
}

class PReadDialog_pCloseButton_actionAdapter implements ActionListener {
	private ReadDialog adaptee;

	PReadDialog_pCloseButton_actionAdapter(ReadDialog adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.pCloseButton_actionPerformed(e);
	}
}

class PReadDialog_pShowErrorOnlyCheckBox_actionAdapter implements ActionListener {
	private ReadDialog adaptee;

	PReadDialog_pShowErrorOnlyCheckBox_actionAdapter(ReadDialog adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.pShowErrorOnlyCheckBox_actionPerformed(e);
	}
}

class PReadDialog_pPauseButton_actionAdapter implements ActionListener {
	private ReadDialog adaptee;

	PReadDialog_pPauseButton_actionAdapter(ReadDialog adaptee) {
		this.adaptee = adaptee;
	}

	public void actionPerformed(ActionEvent e) {
		adaptee.pPauseButton_actionPerformed(e);
	}
}
